package yxy.game.pm2.module.shop.handle;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.heroshop;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.db.dao.MySQLServerDAO;
import yxy.game.pm2.bean.db.module.server;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.message.ShopMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.player.FunctionHandle;
import yxy.game.pm2.module.shop.Goods;
import yxy.game.pm2.module.shop.GoodsLimitType;
import yxy.game.pm2.module.shop.HeroShopModule;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/** 商店 武将商店 */
public class HeroShopHandle extends ShopHandle<heroshop> {

	/** 手动刷新 */
	public Result refresh(String playerCode) {
		HeroShopModule module = HeroShopModule.get(playerCode);
		for (Goods goods : module.getGoods()) {
			if (goods.sellout(limit(data(goods.getDataId())))) {
				return refresh(playerCode, module);
			}
		}
		return Result.ShopCannotRefresh;// 商店不能刷新
	}

	private Result refresh(String playerCode, HeroShopModule module) {
		Currency cost = HeroShopModule.ShopRefreshCost();// 刷新消费
		try {
			new CurrencyHandle().cost(playerCode, CCReason.商店, cost);
		} catch (CurrencyNotEnoughException e) {
			return new Result(Result.CostFail, e.getType().getNumber(), e.getDataId());// 消费不足
		}
		module.refresh();
		return Result.Success;
	}

	public Result buy(String playerCode, int id, int num, ShopMessage.HeroShopBuyResponse.Builder response) {
		BuyResponse buyResponse = new BuyResponse();
		Result result = buy(playerCode, id, num, buyResponse);
		if (result.equals(Result.Success)) {
			response.setPurchase(buyResponse.getPurchase());
			response.setGoods(buyResponse.getGoods());
			HeroShopModule module = HeroShopModule.get(playerCode);
			boolean refresh = module.update();
			response.setNeedRefresh(refresh);
		}
		return result;
	}

	//根据开服时间和showtime过滤商品是否展示
	public  Collection<ShopMessage.Goods> checkShopItemIsShow(String playerCode) {
		Collection<ShopMessage.Goods> goods = new HeroShopHandle().goods(playerCode);

		try {
			//商品表信息
			Map<String, heroshop> datas = DataCenter.getAllData(heroshop.class);

			ArrayList<String> idList = new ArrayList<>();
			//个人的商品信息
			//根据区id获取开服信息,计算已开服日期
			Long alreadlyPassTime = FunctionHandle.getAlreadlyPassTime(playerCode);
			//获取不展示的商品id集合
			Set<Map.Entry<String, heroshop>> entries = datas.entrySet();
			for (Map.Entry<String, heroshop> entry : entries) {
				int showtime = entry.getValue().getShowtime();
				//showtime不等于0且  已开服时间大于showtime才展示商品
				if (alreadlyPassTime - showtime < 0 || showtime == -1) {
					idList.add(entry.getKey());
				}
			}


			//移除对应的不展示商品
			Iterator<ShopMessage.Goods> iterator = goods.iterator();
			while (iterator.hasNext()) {
				ShopMessage.Goods good = iterator.next();
				int id = good.getId();
				if(idList.contains(String.valueOf(id))){
					iterator.remove();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return goods;
	}

	public Long getAlreadlyPassTime(String playerCode) throws ParseException {
		//用户信息
		PlayerBean playerBean = PlayerBean.get(playerCode);
		//服id
		int serverId = playerBean.getServerId();
		//区服信息
		server server = MySQLServerDAO.getFromDAOAccountManager(serverId);
		//开服日期
		Date openTime = server.getOpenTime();
		//当前日期
		Date nowTime = new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format1 = simpleDateFormat.format(nowTime);
		String format2 = simpleDateFormat.format(openTime);
		Date now = simpleDateFormat.parse(format1);
		Date before = simpleDateFormat.parse(format2);
		long sub = Math.abs(now.getTime() - before.getTime());
		sub = sub / 1000 / 60 / 60 / 24;
		return sub;
	}

	@Override
	public HeroShopModule getModule(String playerCode) {
		HeroShopModule module = HeroShopModule.get(playerCode);
		module.update();
		return module;
	}

	@Override
	public GoodsLimitType limitType(heroshop data) {
		return GoodsLimitType.forNumber(data.getLimittype());
	}

	@Override
	public int limit(heroshop data) {
		return data.getLimit();
	}

	@Override
	public int rebate(heroshop data) {
		return data.getReduce();
	}

	@Override
	public Currency cost(heroshop data, int num) {
		return Currency.create(data.getCosttype(), data.getCostid(), data.getCostnum() * num);
	}

	@Override
	public Currency purchase(heroshop data, int num) {
		return Currency.create(data.getShoptype(), data.getShopid(), data.getShopnum() * num);
	}

	@Override
	public Class<heroshop> clazz() {
		return heroshop.class;
	}
}